package com.ssd.interceptor;


import com.ssd.handler.UserInfoHandler;
import com.ssd.utils.Base64Utils;
import com.ssd.utils.JsonUtils;
import com.ssd.view.TokenObject;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;


/**
 * @author itcast
 */
@Slf4j
// 当Spring上下文中存在UserInfoHandler和RequestIdHandler时，条件性地创建FeignInterceptor
@ConditionalOnBean(value = {UserInfoHandler.class})
public class FeignInterceptor implements RequestInterceptor {
    private final UserInfoHandler userInfoHandler;


    // 构造函数注入UserInfoHandler和RequestIdHandler
    public FeignInterceptor(UserInfoHandler userInfoHandler) {
        this.userInfoHandler = userInfoHandler;
    }
    /**
     * 将用户信息base64格式编码，传递到下一个微服务
     * @param requestTemplate
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 1.用户信息
        // 获取当前用户信息并进行Base64编码，然后设置到请求头中
        String token = userInfoHandler.currentTokenInfo();
        requestTemplate.header("Authorization", token);
    }
}

